//
// Created by hunan on 25-7-19.
//
#include "download_task.h"
#include <cstring>
#include <unistd.h>
#include "public_module/log_module/log_public_module.h"

void Usage(const char * name)
{
    LOG_PUBLIC_MODULE("usage: %s -i url -o output_file", name);
    LOG_PUBLIC_MODULE("example: %s -i https://www.baidu.com/index.html -o index.html", name);
}

int main(int argc, char * argv[])
{
    char ch = {};
    std::string input;
    std::string output;
    int connection_num = 1;
    int log_mode;
    while ((ch = getopt(argc, argv, "i:o:n:l:h")) != -1)
    {
        switch (ch)
        {
            case 'i':
            {
                input = optarg ? optarg : "";
                break;
            }
            case 'o':
            {
                output = optarg ? optarg : "";
                break;
            }
            case 'n':
            {
                connection_num = atoi(optarg);
                break;
            }
            case 'l':
            {
                log_mode = atoi(optarg);
                break;
            }
            case 'h':
            {
                Usage(argv[0]);
                exit(EXIT_FAILURE);
            }
            default:
                break;
        }
        ch = {};
    }
    if (!input.empty() && !output.empty())
    {
        LOG_INIT_MODE(log_mode == 0 ? LOG_PRINT_MODE : LOG_FILE_MODE);
        LOG_UPDATE_MODULE(SOCKET_MODULE_NAME, 0);
        DownloadTask task = {};
        LOG_PUBLIC_MODULE("DownloadTask(%p) main input:%s,conn number:%d,output:%s", &task, input.c_str(), connection_num, output.c_str());
        task.Download(input, output, connection_num);
    }
    else
    {
        Usage(argv[0]);
    }
    return EXIT_SUCCESS;
}